package com.ywu.bpmn.flow.service;

import com.ywu.boot.context.YwuContext;
import com.ywu.boot.context.YwuContextHolder;
import com.ywu.bpmn.flow.build.YwuBpmnBuildService;
import com.ywu.bpmn.flow.data.YwuFlowDefinitionRepository;
import com.ywu.bpmn.flow.data.YwuFlowDefinitionVersionRepository;
import com.ywu.bpmn.flow.dto.YwuFlowDefinitionDto;
import com.ywu.bpmn.flow.entity.YwuFlowDefinitionEntity;
import com.ywu.bpmn.flow.entity.YwuFlowDefinitionVersionEntity;
import com.ywu.bpmn.form.service.dto.YwuFormDefinitionDto;
import com.ywu.bpmn.form.service.entity.YwuFormDefinitionEntity;
import com.ywu.bpmn.form.service.service.YwuFormDefinitionService;
import com.ywu.common.entity.PageList;
import com.ywu.common.page.PageService;
import com.ywu.common.result.CommonResult;
import com.ywu.common.utils.TimerIdGenerateUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @ClassName YwuFlowDefinitionService
 * @Description TODO
 * @Author GroundDemo
 * @Date 2025/4/22 17:09
 * @Version 1.0
 **/
@Service
public class YwuFlowDefinitionService {
    @Autowired
    private YwuFlowDefinitionRepository repository;

    @Autowired
    private YwuFlowDefinitionVersionRepository versionRepository;

    @Autowired
    private YwuFormDefinitionService formDefinitionService;

    @Autowired
    private YwuBpmnBuildService bpmnBuildService;

    public static final String DEFAULT_FLOW_JSON = "{\n" +
            "  \"id\": \"nid-start\",\n" +
            "  \"type\": \"YwuStartNode\",\n" +
            "  \"name\": \"开始\",\n" +
            "  \"properties\": {},\n" +
            "  \"next\": {\n" +
            "    \"id\": \"nid-end\",\n" +
            "    \"type\": \"YwuEndNode\",\n" +
            "    \"name\": \"结束\",\n" +
            "    \"properties\": {}\n" +
            "  }\n" +
            "}";

    /**
    * @Author GroundDemo
    * @Description 查询当前租户下是否存在该流程
    * @Date 17:44 2025/4/22
    * @Param [dto, isQueryJson]
    * @return com.ywu.bpmn.flow.entity.YwuFlowDefinitionEntity
    **/
    public YwuFlowDefinitionEntity queryEntityByTenantId(YwuFlowDefinitionDto dto, boolean isQueryJson) {
        YwuContext currentContext = YwuContextHolder.getCurrentContext();
        YwuFlowDefinitionEntity search = new YwuFlowDefinitionEntity();
        search.setId(dto.getId());
        search.setTenantId(currentContext.getYwuContextTenant().getTenantId());
        YwuFlowDefinitionEntity queryRes;
        if (isQueryJson) {
            queryRes = repository.getEntity(search);
        } else {
            queryRes = repository.getEntityNoJson(search);
        }
        if (Objects.isNull(queryRes)) {
            return null;
        } else {
            return queryRes;
        }
    }

    /**
    * @Author GroundDemo
    * @Description 创建流程
    * @Date 17:13 2025/4/22
    * @Param [dto]
    * @return com.ywu.common.result.CommonResult<java.lang.Object>
    **/
    @Transactional
    public CommonResult<Object> createFlowInfo(YwuFlowDefinitionDto dto) {
        // 校验编码是否存在
        YwuFlowDefinitionEntity search = new YwuFlowDefinitionEntity();
        search.setCode(dto.getCode());
        YwuFlowDefinitionEntity queryRes = repository.getEntityNoJson(search);
        if (Objects.nonNull(queryRes)) {
            return CommonResult.error().setErrorCode("code.has.exist");
        }
        // 构建表单对象并创建
        YwuFormDefinitionDto formEntity = new YwuFormDefinitionDto();
        formEntity.setId(TimerIdGenerateUtil.nextId());
        formEntity.setCode("flow." + dto.getCode());
        formEntity.setNameCn("流程." + dto.getNameCn());
        formEntity.setNameEn("flow." + dto.getNameEn());
        formEntity.setDescription(dto.getDescription());
        formEntity.setSource("FLOW");
        CommonResult<Object> formRes = formDefinitionService.createDefinition(formEntity);
        if (Objects.isNull(formRes) || !"200".equals(formRes.getCode())) {
            return CommonResult.error().setErrorCode("form.create.error");
        }
        String formId = (String) formRes.getData();
        // 构建插入对象
        YwuContext currentContext = YwuContextHolder.getCurrentContext();
        YwuFlowDefinitionEntity flowEntity = new YwuFlowDefinitionEntity();
        BeanUtils.copyProperties(dto, flowEntity);
        flowEntity.setId(TimerIdGenerateUtil.nextId());
        flowEntity.createInfo(currentContext.getYwuContextUser().getUserAccount());
        flowEntity.setFormId(formId);
        // 初始化版本信息
        flowEntity.setStatus(YwuFlowDefinitionEntity.DRAFT_STATUS);
        flowEntity.setHasNotReleased(0);
        flowEntity.setCurrentVersion(0);
        flowEntity.setTenantId(currentContext.getYwuContextTenant().getTenantId());
        flowEntity.setFlowJson(DEFAULT_FLOW_JSON);
        // 插入数据
        repository.insert(flowEntity);
        return CommonResult.success(flowEntity.getId());
    }

    /**
    * @Author GroundDemo
    * @Description 修改流程元数据信息
    * @Date 17:39 2025/4/22
    * @Param [dto]
    * @return com.ywu.common.result.CommonResult<java.lang.Object>
    **/
    @Transactional
    public CommonResult<Object> updateFlowMetaData(YwuFlowDefinitionDto dto) {
        // 校验是否有操作权限（当前租户下是否存在）
        YwuFlowDefinitionEntity queryRes = queryEntityByTenantId(dto, true);
        if (Objects.isNull(queryRes)) {
            return CommonResult.error().setErrorCode("id.is.not.exist");
        }
        // 修改表单数据
        YwuFormDefinitionDto formEntity = new YwuFormDefinitionDto();
        formEntity.setId(queryRes.getFormId());
        formEntity.setCode("flow." + dto.getCode());
        formEntity.setNameCn("流程." + dto.getNameCn());
        formEntity.setNameEn("flow." + dto.getNameEn());
        formEntity.setSource("FLOW");
        formEntity.setDescription(dto.getDescription());
        CommonResult<Object> result = formDefinitionService.updateDefinition(formEntity);
        if (Objects.isNull(result) || !"200".equals(result.getCode())) {
            return CommonResult.error().setErrorCode("form.update.error");
        }
        YwuContext currentContext = YwuContextHolder.getCurrentContext();
        // 如果当前数据是草稿，则直接修改当前数据即可
        if (queryRes.getStatus().equals(YwuFlowDefinitionEntity.DRAFT_STATUS)) {
            // 修改数据
            YwuFlowDefinitionEntity update = new YwuFlowDefinitionEntity();
            BeanUtils.copyProperties(dto, update);
            update.setId(queryRes.getId());
            update.setStatus(queryRes.getStatus());
            update.setCurrentVersion(queryRes.getCurrentVersion());
            update.setHasNotReleased(queryRes.getHasNotReleased());
            update.updateInfo(currentContext.getYwuContextUser().getUserAccount());
            repository.updateMetadata(update);
        } else {
            // 当前版本已经发布，需要重新生成一个草稿版本
            YwuFlowDefinitionVersionEntity versionEntity = new YwuFlowDefinitionVersionEntity();
            BeanUtils.copyProperties(dto, versionEntity);
            versionEntity.setId(TimerIdGenerateUtil.nextId());
            versionEntity.setResourceId(queryRes.getId());
            versionEntity.setVersion(0);
            versionEntity.createInfo(currentContext.getYwuContextUser().getUserAccount());
            versionEntity.setFormId(queryRes.getFormId());
            // 如果当前数据已经存在了草稿版本，则需要查询出草稿版本的数据，以草稿版本的数据作为填充
            if (queryRes.getHasNotReleased() == 1) {
                YwuFlowDefinitionVersionEntity searchVersionEntity = new YwuFlowDefinitionVersionEntity();
                searchVersionEntity.setResourceId(queryRes.getId());
                searchVersionEntity.setVersion(0);
                YwuFlowDefinitionVersionEntity queryVersionEntity = versionRepository.getEntity(searchVersionEntity);
                versionEntity.setFlowJson(queryVersionEntity.getFlowJson());
            } else {
                versionEntity.setFlowJson(queryRes.getFlowJson());
            }
            versionEntity.setTenantId(currentContext.getYwuContextTenant().getTenantId());
            // 采取先删除，再插入的方式
            YwuFlowDefinitionVersionEntity delete = new YwuFlowDefinitionVersionEntity();
            delete.setResourceId(queryRes.getId());
            delete.setVersion(0);
            versionRepository.delete(delete);
            versionRepository.insert(versionEntity);
            // 更新当前流程的状态
            queryRes.setHasNotReleased(1);
            queryRes.updateInfo(currentContext.getYwuContextUser().getUserAccount());
            repository.updateVersionInfo(queryRes);
        }
        return CommonResult.success(null);
    }

    /**
    * @Author GroundDemo
    * @Description 修改流程json信息
    * @Date 18:05 2025/4/22
    * @Param [dto]
    * @return com.ywu.common.result.CommonResult<java.lang.Object>
    **/
    @Transactional
    public CommonResult<Object> updateFlowJson(YwuFlowDefinitionDto dto) {
        // 校验是否有操作权限（当前租户下是否存在）
        YwuFlowDefinitionEntity queryRes = queryEntityByTenantId(dto, false);
        if (Objects.isNull(queryRes)) {
            return CommonResult.error().setErrorCode("id.is.not.exist");
        }
        // 修改表单数据----实际上不需要修改表单数据，但是为了保持流程是表单的版本一致，此处选择生成表单的草稿版本
        YwuFormDefinitionDto formEntity = new YwuFormDefinitionDto();
        formEntity.setCode("flow." + queryRes.getCode());
        formEntity.setId(queryRes.getFormId());
        formEntity.setNameEn("flow." + queryRes.getNameEn());
        formEntity.setNameCn("流程." + queryRes.getNameCn());
        formEntity.setDescription(queryRes.getDescription());
        formEntity.setSource("FLOW");
        CommonResult<Object> result = formDefinitionService.updateDefinition(formEntity);
        if (Objects.isNull(result) || !"200".equals(result.getCode())) {
            return CommonResult.error().setErrorCode("form.update.error");
        }
        YwuContext currentContext = YwuContextHolder.getCurrentContext();
        // 如果当前数据是草稿，则直接修改当前数据即可
        if (queryRes.getStatus().equals(YwuFlowDefinitionEntity.DRAFT_STATUS)) {
            YwuFlowDefinitionEntity update = new YwuFlowDefinitionEntity();
            update.setFlowJson(dto.getFlowJson());
            update.setId(dto.getId());
            update.setStatus(queryRes.getStatus());
            update.setCurrentVersion(queryRes.getCurrentVersion());
            update.setHasNotReleased(queryRes.getHasNotReleased());
            update.updateInfo(currentContext.getYwuContextUser().getUserAccount());
            repository.updateFlowJson(update);
        } else {
            // 当前版本已经发布，需要重新生成一个草稿版本
            YwuFlowDefinitionVersionEntity versionEntity = new YwuFlowDefinitionVersionEntity();
            if (queryRes.getHasNotReleased() == 1) {
                // 存在草稿版本，需要以草稿版本为准，查询草稿版本数据进行填充
                YwuFlowDefinitionVersionEntity searchVersionEntity = new YwuFlowDefinitionVersionEntity();
                searchVersionEntity.setResourceId(queryRes.getId());
                searchVersionEntity.setVersion(0);
                YwuFlowDefinitionVersionEntity queryVersionEntity = versionRepository.getEntity(searchVersionEntity);
                BeanUtils.copyProperties(queryVersionEntity, versionEntity);
            } else {
                // 以原始数据进行填充
                BeanUtils.copyProperties(queryRes, versionEntity);
            }
            versionEntity.setId(TimerIdGenerateUtil.nextId());
            versionEntity.setResourceId(queryRes.getId());
            versionEntity.setVersion(0);
            versionEntity.createInfo(currentContext.getYwuContextUser().getUserAccount());
            versionEntity.setFlowJson(dto.getFlowJson());
            versionEntity.setTenantId(currentContext.getYwuContextTenant().getTenantId());
            // 采取先删除，再插入的方式
            YwuFlowDefinitionVersionEntity delete = new YwuFlowDefinitionVersionEntity();
            delete.setVersion(0);
            delete.setResourceId(queryRes.getId());
            versionRepository.delete(delete);
            versionRepository.insert(versionEntity);
            // 更新当前流程的状态
            queryRes.setHasNotReleased(1);
            queryRes.updateInfo(currentContext.getYwuContextUser().getUserAccount());
            repository.updateVersionInfo(queryRes);
        }
        return CommonResult.success(null);
    }

    /**
    * @Author GroundDemo
    * @Description 更新流程表单信息
    * @Date 18:12 2025/4/22
    * @Param [dto]
    * @return com.ywu.common.result.CommonResult<java.lang.Object>
    **/
    @Transactional
    public CommonResult<Object> updateFormJson(YwuFlowDefinitionDto dto) {
        // 校验是否有操作权限（当前租户下是否存在）
        YwuFlowDefinitionEntity queryRes = queryEntityByTenantId(dto, true);
        if (Objects.isNull(queryRes)) {
            return CommonResult.error().setErrorCode("id.is.not.exist");
        }
        // 调用表单服务，更新表单json信息
        YwuFormDefinitionDto formDefinitionDto = new YwuFormDefinitionDto();
        formDefinitionDto.setFormJson(dto.getFormJson());
        formDefinitionDto.setId(queryRes.getFormId());
        CommonResult<Object> result = formDefinitionService.updateFormJson(formDefinitionDto);
        if (Objects.isNull(result) || !"200".equals(result.getCode())) {
            return CommonResult.error().setErrorCode("form.json.update.error");
        }
        // 修改当前流程的数据（草稿状态）
        if (queryRes.getStatus().equals(YwuFlowDefinitionEntity.ONLINE_STATUS)) {
            YwuContext currentContext = YwuContextHolder.getCurrentContext();
            // 如果当前是已经发布的版本，则需要重新添加一条草稿版本的数据
            YwuFlowDefinitionVersionEntity versionEntity = new YwuFlowDefinitionVersionEntity();
            if (queryRes.getHasNotReleased() == 1) {
                // 存在草稿版本，需要以草稿版本为准，查询草稿版本数据进行填充
                YwuFlowDefinitionVersionEntity searchVersionEntity = new YwuFlowDefinitionVersionEntity();
                searchVersionEntity.setVersion(0);
                searchVersionEntity.setResourceId(queryRes.getId());
                YwuFlowDefinitionVersionEntity queryVersionEntity = versionRepository.getEntity(searchVersionEntity);
                BeanUtils.copyProperties(queryVersionEntity, versionEntity);
            } else {
                // 以原始数据进行填充
                BeanUtils.copyProperties(queryRes, versionEntity);
            }
            versionEntity.setId(TimerIdGenerateUtil.nextId());
            versionEntity.setResourceId(queryRes.getId());
            versionEntity.setVersion(0);
            versionEntity.createInfo(currentContext.getYwuContextUser().getUserAccount());
            versionEntity.setTenantId(currentContext.getYwuContextTenant().getTenantId());
            // 更新当前流程的状态
            queryRes.setHasNotReleased(1);
            queryRes.updateInfo(currentContext.getYwuContextUser().getUserAccount());
            repository.updateVersionInfo(queryRes);
            // 采取先删除，再插入的方式
            YwuFlowDefinitionVersionEntity delete = new YwuFlowDefinitionVersionEntity();
            delete.setVersion(0);
            delete.setResourceId(queryRes.getId());
            versionRepository.delete(delete);
            versionRepository.insert(versionEntity);
        }
        return CommonResult.success(null);
    }

    /**
    * @Author GroundDemo
    * @Description 删除流程
    * @Date 18:23 2025/4/22
    * @Param [dto]
    * @return com.ywu.common.result.CommonResult<java.lang.Object>
    **/
    @Transactional
    public CommonResult<Object> deleteFlowInfo(YwuFlowDefinitionDto dto) {
        // 校验是否有操作权限（当前租户下是否存在）
        YwuFlowDefinitionEntity queryRes = queryEntityByTenantId(dto, false);
        if (Objects.isNull(queryRes)) {
            return CommonResult.error().setErrorCode("id.is.not.exist");
        }
        // 删除流程
        YwuFlowDefinitionEntity delete = new YwuFlowDefinitionEntity();
        delete.setId(dto.getId());
        repository.delete(delete);
        // 删除流程的历史版本
        YwuFlowDefinitionVersionEntity deleteVersion = new YwuFlowDefinitionVersionEntity();
        deleteVersion.setResourceId(dto.getId());
        versionRepository.delete(deleteVersion);
        // 同步删除表单数据
        YwuFormDefinitionDto deleteForm = new YwuFormDefinitionDto();
        deleteForm.setId(queryRes.getFormId());
        formDefinitionService.deleteFormDefinition(deleteForm);
        return CommonResult.success(null);
    }

    /**
     * @Author GroundDemo
     * @Description 分页查询流程信息
     * @Date 18:33 2025/4/22
     * @Param [dto]
     * @return com.ywu.common.result.CommonResult<java.lang.Object>
     **/
    public CommonResult<Object> queryFlowPageList(YwuFlowDefinitionDto dto) {
        // 构建分页
        YwuContext currentContext = YwuContextHolder.getCurrentContext();
        PageService.execPageStartAndEnd(dto.getPageInfo());
        dto.setTenantId(currentContext.getYwuContextTenant().getTenantId());
        // 查询所有数量
        int count = repository.queryCount(dto);
        if (count == 0) {
            PageList<YwuFlowDefinitionEntity> pageList = new PageList<>(dto.getPageInfo().getPageIndex(),
                    dto.getPageInfo().getPageSize(), count, new ArrayList<>());
            return CommonResult.success(pageList);
        }
        // 查询所有数据
        List<YwuFlowDefinitionEntity> queryList = repository.queryPageList(dto);
        // 批量查询需要填充草稿版本的信息
        List<String> ids = queryList.stream().filter(res ->
                        res.getStatus().equals(YwuFlowDefinitionEntity.ONLINE_STATUS) && res.getHasNotReleased() == 1)
                .map(YwuFlowDefinitionEntity::getId).collect(Collectors.toList());
        if (!CollectionUtils.isEmpty(ids)) {
            // 批量查询草稿版本
            List<YwuFlowDefinitionVersionEntity> draftVersionList = versionRepository.queryDraftVersionList(ids);
            // 构建map，方便后续填充
            Map<String, YwuFlowDefinitionVersionEntity> draftMap = draftVersionList.stream()
                    .collect(Collectors.toMap(YwuFlowDefinitionVersionEntity::getResourceId,
                            res -> res, (v1, v2) -> v1));
            // 填充数据
            for (YwuFlowDefinitionEntity definitionEntity : queryList) {
                if (draftMap.containsKey(definitionEntity.getId())) {
                    YwuFlowDefinitionVersionEntity draftInfo = draftMap.get(definitionEntity.getId());
                    BeanUtils.copyProperties(draftInfo, definitionEntity, "id", "createBy", "createDate", "updateBy", "updateDate");
                    definitionEntity.setId(draftInfo.getResourceId());
                }
            }
        }
        PageList<YwuFlowDefinitionEntity> pageList = new PageList<>(dto.getPageInfo().getPageIndex(),
                dto.getPageInfo().getPageSize(), count, queryList);
        return CommonResult.success(pageList);
    }

    /**
    * @Author GroundDemo
    * @Description 发布流程
    * @Date 18:28 2025/4/22
    * @Param [dto]
    * @return com.ywu.common.result.CommonResult<java.lang.Object>
    **/
    @Transactional
    public CommonResult<Object> publishFlowInfo(YwuFlowDefinitionDto dto) {
        // 校验是否有操作权限（当前租户下是否存在）
        YwuFlowDefinitionEntity queryRes = queryEntityByTenantId(dto, true);
        if (Objects.isNull(queryRes)) {
            return CommonResult.error().setErrorCode("id.is.not.exist");
        }
        YwuContext currentContext = YwuContextHolder.getCurrentContext();
        // 同步发布表单
        YwuFormDefinitionDto ywuFormDefinitionDto = new YwuFormDefinitionDto();
        ywuFormDefinitionDto.setId(queryRes.getFormId());
        formDefinitionService.publishForm(ywuFormDefinitionDto);
        YwuFlowDefinitionEntity bpmnEntity = new YwuFlowDefinitionEntity();
        // 如果当前状态时草稿，则需要将当前数据的状态改为已发布
        if (queryRes.getStatus().equals(YwuFlowDefinitionEntity.DRAFT_STATUS)) {
            YwuFlowDefinitionEntity update = new YwuFlowDefinitionEntity();
            update.setId(dto.getId());
            update.setHasNotReleased(0);
            update.updateInfo(currentContext.getYwuContextUser().getUserAccount());
            update.setStatus(YwuFormDefinitionEntity.ONLINE_STATUS);
            update.setCurrentVersion(queryRes.getCurrentVersion() + 1);
            repository.updateVersionInfo(update);
            // 添加一条版本记录
            YwuFlowDefinitionVersionEntity insertVersion = new YwuFlowDefinitionVersionEntity();
            BeanUtils.copyProperties(queryRes, insertVersion);
            insertVersion.setId(TimerIdGenerateUtil.nextId());
            insertVersion.createInfo(currentContext.getYwuContextUser().getUserAccount());
            insertVersion.setResourceId(queryRes.getId());
            insertVersion.setVersion(queryRes.getCurrentVersion() + 1);
            versionRepository.insert(insertVersion);
            bpmnEntity.setId(queryRes.getId());
            bpmnEntity.setFlowJson(queryRes.getFlowJson());
            bpmnEntity.setNameCn(queryRes.getNameCn());
        } else {
            // 当前状态为已发布，需要读取草稿版本的数据，将其数据填充到原始表中
            YwuFlowDefinitionVersionEntity searchEntity = new YwuFlowDefinitionVersionEntity();
            searchEntity.setResourceId(queryRes.getId());
            searchEntity.setVersion(0);
            YwuFlowDefinitionVersionEntity versionEntity = versionRepository.getEntity(searchEntity);
            if (Objects.isNull(versionEntity)) {
                // 不存在草稿版本，无法发布
                return CommonResult.error().setErrorCode("draft.version.is.not.exist");
            }
            YwuFlowDefinitionEntity newFormEntity = new YwuFlowDefinitionEntity();
            BeanUtils.copyProperties(versionEntity, newFormEntity);
            newFormEntity.setId(queryRes.getId());
            newFormEntity.updateInfo(currentContext.getYwuContextUser().getUserAccount());
            newFormEntity.setCurrentVersion(queryRes.getCurrentVersion() + 1);
            newFormEntity.setHasNotReleased(0);
            newFormEntity.setStatus(queryRes.getStatus());
            repository.updateAll(newFormEntity);
            // 将原始那条草稿版本的数据的版本号改为最新的版本号
            versionEntity.setVersion(queryRes.getCurrentVersion() + 1);
            versionRepository.updateVersion(versionEntity);
            bpmnEntity.setId(queryRes.getId());
            bpmnEntity.setFlowJson(versionEntity.getFlowJson());
            bpmnEntity.setNameCn(versionEntity.getNameCn());
        }
        // 构建BPMN，发布到activiti
        bpmnBuildService.publishBpmn(bpmnEntity);
        return CommonResult.success(null);
    }

    /**
    * @Author GroundDemo
    * @Description 查询流程详情
    * @Date 21:36 2025/4/22
    * @Param [dto]
    * @return com.ywu.common.result.CommonResult<java.lang.Object>
    **/
    public CommonResult<Object> queryFlowInfo(YwuFlowDefinitionDto dto) {
        YwuFlowDefinitionEntity search = new YwuFlowDefinitionEntity();
        YwuContext currentContext = YwuContextHolder.getCurrentContext();
        search.setId(dto.getId());
        search.setTenantId(currentContext.getYwuContextTenant().getTenantId());
        YwuFlowDefinitionEntity entity = repository.getEntity(search);
        return CommonResult.success(entity);
    }

    /**
    * @Author GroundDemo
    * @Description 查询流程表单内容
    * @Date 22:34 2025/4/22
    * @Param [dto]
    * @return com.ywu.common.result.CommonResult<java.lang.Object>
    **/
    public CommonResult<Object> queryFlowFormInfo(YwuFlowDefinitionDto dto) {
        // 查询当前流程
        YwuFlowDefinitionEntity search = new YwuFlowDefinitionEntity();
        YwuContext currentContext = YwuContextHolder.getCurrentContext();
        search.setId(dto.getId());
        search.setTenantId(currentContext.getYwuContextTenant().getTenantId());
        YwuFlowDefinitionEntity entity = repository.getEntity(search);
        // 如果为空，返回异常
        if (Objects.isNull(entity)) {
            return CommonResult.error().setErrorCode("id.is.not.exist");
        }
        // 根据表单id查询表单信息
        YwuFormDefinitionDto searchForm = new YwuFormDefinitionDto();
        searchForm.setId(entity.getFormId());
        CommonResult<Object> result = formDefinitionService.queryFormEntity(searchForm);
        if (Objects.isNull(result) || !"200".equals(result.getCode())) {
            return CommonResult.error().setErrorCode("form.info.query.error");
        }
        return result;
    }
}
